﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sharp.Portal.Mvc.Bootstrap;
using Sharp.Portal.Mvc.Models;

namespace Sharp.Portal.Mvc.Bootstrap
{
    /// <summary>
    /// 控件创建器扩展
    /// </summary>
    public static class BuilderExtension
    {

        /// <summary>
        /// 代码项转换文本项
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="category"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static LableBuilder Code2Text(this LableBuilder helper, string category, string code)
        {
            var data = Sharp.Portal.Platform.GetMasterData(category);
            var value = data.FirstOrDefault(x => x.Category == category && x.Code == code)?.Name;

            helper.Text(value);
            return helper;
        }
        /// <summary>
        /// 值转换为文字
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="data"></param>
        /// <param name="value"></param>
        /// <param name="displayField"></param>
        /// <param name="valueField"></param>
        /// <returns></returns>
        public static LableBuilder Value2Text(this LableBuilder helper, object data, object value,
            string displayField, string valueField)
        {

            //if (data == null)
            //{
            //    helper.Text("");
            //    return helper;
            //}
            //foreach (var item in data as IEnumerable)
            //{
            //    var displayFieldProperty = item.GetType().GetProperty(displayField);
            //    var valueFieldProperty = item.GetType().GetProperty(valueField);

            //    if (Convert.ToString(value).Equals(valueFieldProperty.GetValue(item, null).ToString(),
            //        StringComparison.CurrentCultureIgnoreCase))
            //    {
            //        value = displayFieldProperty.GetValue(item, null).ToString();

            //        helper.Text(Convert.ToString(value));
            //        return helper;
            //    }
            //}
            //helper.Text(Convert.ToString(value));

            helper.Text("testing");
            return helper;
        }
        /// <summary>
        /// 节点的Id转换为节点文字项
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="data"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static LableBuilder NodeId2NodeText(this LableBuilder helper, zTreeNode data, string id)
        {
            if (id.IsNullOrEmpty())
                return helper;

            var sb = new StringBuilder();
            data.GetText(sb, id);
            return helper.Text(sb.ToString());
        }

        private static void GetText(this zTreeNode data, StringBuilder sb, string id)
        {
            foreach (var node in data.Children)
            {
                if (node.Id == id)
                {
                    sb.Append(node.Name);
                    break;
                }

                node.GetText(sb, id);
            }
        }
    }
}